<?php


namespace IDock\Insurance\TAIC;

use IDock\Http\Response;
use IDock\IDock\IDockInterface;
use IDock\Insurance\TAIC\Request\PolicyPaymentRequest;
use IDock\Insurance\TAIC\Request\PremiumInquiryRequest;
use IDock\Insurance\TAIC\Request\ProposalStatusRequest;
use IDock\Insurance\TAIC\Request\RenewalInquiryRequest;
use IDock\Insurance\TAIC\Request\UnderwritingRequest;
use IDock\Insurance\TAIC\Request\UploadFileRequest;
use IDock\Insurance\TAIC\Request\VehicleModelRequest;
use IDock\Insurance\TAIC\Response\NonCarProductListResponse;
use IDock\Insurance\TAIC\Response\PolicyPaymentResponse;
use IDock\Insurance\TAIC\Response\PremiumInquiryResponse;
use IDock\Insurance\TAIC\Response\ProposalStatusResponse;
use IDock\Insurance\TAIC\Response\RenewalInquiryResponse;
use IDock\Insurance\TAIC\Response\UnderwritingResponse;
use IDock\Insurance\TAIC\Response\UploadFileResponse;
use IDock\Insurance\TAIC\Response\VehicleModelResponse;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;

class TAICConstruct extends Builder implements IDockInterface
{
    public $config = [];

    public function __construct($config)
    {
        $this->config = $config;

        $this->openId = $config['openId'] ?? '';
        $this->token = $config['token'] ?? '';
        $this->sign = $config['sign'] ?? '';
        $this->pageUrl = $config['pageUrl'] ?? '';
    }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     * @return RenewalInquiryResponse
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor): RenewalInquiryResponse
    {
        $request = new RenewalInquiryRequest();
        $request->setVisitor($renewalInquiryVisitor);

        /*** @var RenewalInquiryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): VehicleModelResponse
    {
        $request = new VehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);

        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);

        if ($response->printer->toArray()) {
            return  $response;
        } else {
            $request->switch = true;
            /*** @var VehicleModelResponse $response */
            $response = $this->execute($request);
        }
        return $response;
    }

    /**
     * 车辆实际价格
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): Response
    {
        return new Response(200);
    }

    /**
     * 询价前置
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): Response
    {
        return new Response(200);
    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        // TODO: Implement validateCar() method.
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return PremiumInquiryResponse
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): PremiumInquiryResponse
    {
        $request = new PremiumInquiryRequest;
        $request->setVisitor($premiumInquiryVisitor);

        //非车询价
        //$this->nonCarInquiry($premiumInquiryVisitor->getNonCarInquiryVisitor());

        /*** @var PremiumInquiryResponse $response */
        $response = $this->execute($request);
        $response->printer->argc['tradeNo'] = $request->getTradeNo();
        //非车结果回填
        $nonCarInquiryPrinter = new NonCarInquiryPrinter();
        $totalPremium = 0;
        foreach ($premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList() as $item) {
            /*** @var $item NonCarProductVisitor */
            $single = new NonCarInquirySinglePrinter();
            $single->premium = $item->premium;
            $single->planCode = $item->planCode;
            $single->productCode = $item->productCode;
            $single->statusCode = 'success';
            $totalPremium += $single->premium * $item->insuredNum;
            $nonCarInquiryPrinter->noCarInquirySingleAppend($single);
        }
        $nonCarInquiryPrinter->totalPremium = $totalPremium;
        $response->printer->nonCarInquiryPrinter = $nonCarInquiryPrinter;
        return $response;
    }

    /**
     * 核保
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return Response
     */
    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor): Response
    {
        $request = new UnderwritingRequest();
        $request->setVisitor($premiumInquiryVisitor);

        //非车核保
        //$this->nonCarUnderwriting($premiumInquiryVisitor->getNonCarInquiryPrinter());

        /*** @var UnderwritingResponse $response */
        $underwritingResponse = $this->execute($request);
        if (!$underwritingResponse->isSuccess()) {
            return $underwritingResponse;
        }

        //影响资料上传
        if (!$premiumInquiryVisitor->getFileList()) {
            return $underwritingResponse;
        }

        $request = new UploadFileRequest();
        $request->tradeNo = $premiumInquiryVisitor->getExtInfoVisitor()->argc['tradeNo'] ?? '';
        $request->inquiryNo = $underwritingResponse->printer->inquiryNo;

        $request->setVisitor($premiumInquiryVisitor);

        $this->execute($request);

        return $underwritingResponse;
    }

    /**
     * 支付
     * @param PolicyPaymentVisitor $paymentVisitor
     * @return PolicyPaymentResponse
     */
    public function policyPayment(PolicyPaymentVisitor $paymentVisitor): PolicyPaymentResponse
    {
        $request = new PolicyPaymentRequest();
        $request->setVisitor($paymentVisitor);
        $request->setPageUrl($this->pageUrl);

        return $this->payment($request);
    }

    /**
     * 文件上传
     * @param UploadFileVisitor $uploadFileVisitor
     * @return Response
     */
    public function uploadFile(UploadFileVisitor $uploadFileVisitor): Response
    {
        return new Response(200);
    }

    /**
     * 投保单状态查询
     * @param ProposalStatusVisitor $proposalStatusVisitor
     * @return ProposalStatusResponse
     */
    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor): ProposalStatusResponse
    {
        $request = new ProposalStatusRequest();
        $request->setVisitor($proposalStatusVisitor);

        /*** @var ProposalStatusResponse $response */
        $response = $this->execute($request);
        $response->setVisitor($proposalStatusVisitor);
        $response->init();
        return $response;
    }

    /**
     * 撤单接口
     * @param ProposalRecallVisitor $revokeVisitor
     * @return Response
     */
    public function proposalRecall(ProposalRecallVisitor $revokeVisitor): Response
    {
        return new Response(200);
    }

    /**
     * 电子保单下载
     * @param EPolicyDownloadVisitor $EPolicyVisitor
     * @return Response
     */
    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): Response
    {
        return new Response(200);
    }

    /**
     * 核保撤回
     * @param UnderwritingRecallVisitor $recallVisitor
     * @return mixed|void
     */
    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor): Response
    {
        return new Response(200);
    }

    /**
     * 保单详情
     * @param PolicyInfoVisitor $policyInfoVisitor
     * @return Response
     */
    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor): Response
    {
        return new Response(200);
    }

    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): Response
    {
        $response = new NonCarProductListResponse(200);
        $response->seat = $nonCarProductListVisitor->seats;
        $response->printerInit();
        return $response;
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor): Response
    {
        return new Response(200);
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter): Response
    {
        return new Response(200);
    }
}
